package baseKnowledge.niukeTest;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class CreateObjectMethod {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, CloneNotSupportedException, IOException {
        Base base=new Base();

        /**
         * 通过反射进行创建对象
         */
//        Base base1=(Base)Class.forName("src.baseKnowledge.niukeTest").newInstance();
        ArrayListAndLinkedList arrayListAndLinkedList=(ArrayListAndLinkedList)Class
                .forName("baseKnowledge.niukeTest").newInstance();

        Base base1=Base.class.newInstance();

        /**
         * 通过Constructor进行创建
         */
        Constructor<Base> constructor=Base.class.getConstructor();
        Base base2=constructor.newInstance();

        /**
         * 使用clone的方法
         * 使用该方法，不会调用任何构造函数
         * jvm会创建一个对象，将前面对象的内容全部拷贝进去
         * 实现接口Cloneable
         */
        Base base3=(Base)base1.clone();

        /**
         * 使用序列化和反序列化创建对象
         * JVM创建对象，不使用构造函数
         * 实现接口Serilizable
         */
        ObjectInputStream in=new ObjectInputStream(new FileInputStream("data.obj"));
        Base base4=(Base) in.readObject();



    }
}

class Base implements Cloneable, Serializable {

    public Base() {
        super();
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return super.toString();
    }


}